<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Zend_Http_Response - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.http.response.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.http.response.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.http.cookies.html">Zend_Http_Cookie and Zend_Http_CookieJar</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.http.html">Zend_Http</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.http.user-agent.html">Zend_Http_UserAgent</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.http.response" class="section"><div class="info"><h1 class="title">Zend_Http_Response</h1></div>
    

    <div class="section" id="zend.http.response.introduction"><div class="info"><h1 class="title">Introduction</h1></div>
        

        <p class="para">
            <span class="classname">Zend_Http_Response</span> provides easy access to an
            <acronym class="acronym">HTTP</acronym> responses message, as well as a set of static methods for
            parsing <acronym class="acronym">HTTP</acronym> response messages. Usually,
            <span class="classname">Zend_Http_Response</span> is used as an object returned by a
            <span class="classname">Zend_Http_Client</span> request.
        </p>

        <p class="para">
            In most cases, a <span class="classname">Zend_Http_Response</span> object will be instantiated
            using the fromString() method, which reads a string containing a
            <acronym class="acronym">HTTP</acronym> response message, and returns a new
            <span class="classname">Zend_Http_Response</span> object:
            <div class="example" id="zend.http.response.introduction.example-1"><div class="info"><p><b>Example #1 Instantiating a Zend_Http_Response Object Using the Factory Method</b></p></div>
                

                <pre class="programlisting brush: php">
$str = &#039;&#039;;
$sock = fsockopen(&#039;www.example.com&#039;, 80);
$req =     &quot;GET / HTTP/1.1\r\n&quot; .
        &quot;Host: www.example.com\r\n&quot; .
        &quot;Connection: close\r\n&quot; .
        &quot;\r\n&quot;;

fwrite($sock, $req);
while ($buff = fread($sock, 1024))
    $str .= $sock;

$response = Zend_Http_Response::fromString($str);
</pre>

            </div>
        </p>

        <p class="para">
            You can also use the contractor method to create a new response
            object, by specifying all the parameters of the response:
        </p>

        <p class="para">
            <strong class="command">public function __construct($code, $headers, $body = null, $version = &#039;1.1&#039;,
                $message = null)</strong>
        </p>

        <ul class="itemizedlist">
            <li class="listitem">
                <p class="para">
                    <var class="varname">$code</var>: The <acronym class="acronym">HTTP</acronym> response code (eg. 200,
                    404, etc.)
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    <var class="varname">$headers</var>: An associative array of <acronym class="acronym">HTTP</acronym>
                    response headers (eg. &#039;Host&#039; =&gt; &#039;example.com&#039;)
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    <var class="varname">$body</var>: The response body as a string
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    <var class="varname">$version</var>: The <acronym class="acronym">HTTP</acronym> response version
                    (usually 1.0 or 1.1)
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    <var class="varname">$message</var>: The <acronym class="acronym">HTTP</acronym> response message (eg
                    &#039;OK&#039;, &#039;Internal Server Error&#039;). If not specified, the message will be set
                    according to the response code
                </p>
            </li>
        </ul>
    </div>

    <div class="section" id="zend.http.response.testers"><div class="info"><h1 class="title">Boolean Tester Methods</h1></div>
        

        <p class="para">
            Once a <span class="classname">Zend_Http_Response</span> object is instantiated, it provides
            several methods that can be used to test the type of the response. These all
            return Boolean <b><tt>TRUE</tt></b> or <b><tt>FALSE</tt></b>:
            <ul class="itemizedlist">
                <li class="listitem">
                    <p class="para">
                         <span class="methodname">isSuccessful()</span>: Whether the request was successful
                        or not. Returns <b><tt>TRUE</tt></b> for <acronym class="acronym">HTTP</acronym> 1xx
                        and 2xx response codes
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                         <span class="methodname">isError()</span>: Whether the response code implies an
                        error or not. Returns <b><tt>TRUE</tt></b> for <acronym class="acronym">HTTP</acronym>
                        4xx (client errors) and 5xx (server errors) response codes
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                         <span class="methodname">isRedirect()</span>: Whether the response is a redirection
                        response or not. Returns <b><tt>TRUE</tt></b> for
                        <acronym class="acronym">HTTP</acronym> 3xx response codes
                    </p>
                </li>
            </ul>

            <div class="example" id="zend.http.response.testers.example-1"><div class="info"><p><b>Example #2 Using the isError() method to validate a response</b></p></div>
                

                <pre class="programlisting brush: php">
if ($response-&gt;isError()) {
  echo &quot;Error transmitting data.\n&quot;
  echo &quot;Server reply was: &quot; . $response-&gt;getStatus() .
       &quot; &quot; . $response-&gt;getMessage() . &quot;\n&quot;;
}
// .. process the response here...
</pre>

            </div>
        </p>
    </div>

    <div class="section" id="zend.http.response.acessors"><div class="info"><h1 class="title">Accessor Methods</h1></div>
        

        <p class="para">
            The main goal of the response object is to provide easy access to
            various response parameters.
            <ul class="itemizedlist">
                <li class="listitem">
                    <p class="para">
                         <span class="methodname">getStatus()</span>: Get the <acronym class="acronym">HTTP</acronym>
                        response status code (eg. 200, 504, etc.)
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                         <span class="methodname">getMessage()</span>: Get the <acronym class="acronym">HTTP</acronym>
                        response status message (eg. &quot;Not Found&quot;, &quot;Authorization Required&quot;)
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                         <span class="methodname">getBody()</span>: Get the fully decoded
                        <acronym class="acronym">HTTP</acronym> response body
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                         <span class="methodname">getRawBody()</span>: Get the raw, possibly encoded
                        <acronym class="acronym">HTTP</acronym> response body. if the body was decoded using GZIP
                        encoding for example, it will not be decoded.
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                         <span class="methodname">getHeaders()</span>: Get the <acronym class="acronym">HTTP</acronym>
                        response headers as an associative array (eg. &#039;Content-type&#039; =&gt; &#039;text/html&#039;)
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                         <span class="methodname">getHeader($header)</span>: Get a specific
                        <acronym class="acronym">HTTP</acronym> response header, specified by $header
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                         <span class="methodname">getHeadersAsString($status_line, $br)</span>: Get
                        the entire set of headers as a string. If <var class="varname">$status_line</var> is
                        <b><tt>TRUE</tt></b> (default), the first status line (eg. &quot;HTTP/1.1
                        200 OK&quot;) will also be returned. Lines are broken with the
                        <var class="varname">$br</var> parameter (Can be, for example, &quot;&lt;br /&gt;&quot;.
                        Default &quot;\n&quot;)
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                         <span class="methodname">asString($br)</span>: Get the entire response message as
                        a string. Lines are broken with the $br parameter (Can be, for example,
                        &quot;&lt;br /&gt;&quot;. Default &quot;\n&quot;). You can also use the magic method
                         <span class="methodname">__toString()</span> when casting the object as a string.
                        It will then proxy to  <span class="methodname">asString()</span>.
                    </p>
                </li>
            </ul>

            <div class="example" id="zend.http.response.acessors.example-1"><div class="info"><p><b>Example #3 Using Zend_Http_Response Accessor Methods</b></p></div>
                

                <pre class="programlisting brush: php">
if ($response-&gt;getStatus() == 200) {
  echo &quot;The request returned the following information:&lt;br /&gt;&quot;;
  echo $response-&gt;getBody();
} else {
  echo &quot;An error occurred while fetching data:&lt;br /&gt;&quot;;
  echo $response-&gt;getStatus() . &quot;: &quot; . $response-&gt;getMessage();
}
</pre>

            </div>

            <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>Always check return value</b><br /></span>
                

                <p class="para">
                    Since a response can contain several instances of the same header,
                    the getHeader() method and getHeaders() method may return either a
                    single string, or an array of strings for each header. You should
                    always check whether the returned value is a string or array.
                </p>
            </p></blockquote>

            <div class="example" id="zend.http.response.acessors.example-2"><div class="info"><p><b>Example #4 Accessing Response Headers</b></p></div>
                

                <pre class="programlisting brush: php">
$ctype = $response-&gt;getHeader(&#039;Content-type&#039;);
if (is_array($ctype)) $ctype = $ctype[0];

$body = $response-&gt;getBody();
if ($ctype == &#039;text/html&#039; || $ctype == &#039;text/xml&#039;) {
  $body = htmlentities($body);
}

echo $body;
</pre>

            </div>
        </p>
    </div>

    <div class="section" id="zend.http.response.static_parsers"><div class="info"><h1 class="title">Static HTTP Response Parsers</h1></div>
        

        <p class="para">
            The <span class="classname">Zend_Http_Response</span> class also includes several
            internally-used methods for processing and parsing <acronym class="acronym">HTTP</acronym> response
            messages. These methods are all exposed as static methods, which means they can be
            used externally, even if you do not need to instantiate a response
            object, and just want to extract a specific part of the response.

            <ul class="itemizedlist">
                <li class="listitem">
                    <p class="para">
                         <span class="methodname">Zend_Http_Response::extractCode($response_str)</span>:
                        Extract and return the <acronym class="acronym">HTTP</acronym> response code (eg. 200 or
                        404) from <var class="varname">$response_str</var>
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                         <span class="methodname">Zend_Http_Response::extractMessage($response_str)</span>:
                        Extract and return the <acronym class="acronym">HTTP</acronym> response message (eg. &quot;OK&quot; or
                        &quot;File Not Found&quot;) from <var class="varname">$response_str</var>
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                         <span class="methodname">Zend_Http_Response::extractVersion($response_str)</span>:
                        Extract and return the <acronym class="acronym">HTTP</acronym> version (eg. 1.1 or 1.0) from
                        <var class="varname">$response_str</var>
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                         <span class="methodname">Zend_Http_Response::extractHeaders($response_str)</span>:
                        Extract and return the <acronym class="acronym">HTTP</acronym> response headers from
                        <var class="varname">$response_str</var> as an array
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                         <span class="methodname">Zend_Http_Response::extractBody($response_str)</span>:
                        Extract and return the <acronym class="acronym">HTTP</acronym> response body from
                        <var class="varname">$response_str</var>
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                         <span class="methodname">Zend_Http_Response::responseCodeAsText($code,
                            $http11)</span>: Get the standard <acronym class="acronym">HTTP</acronym> response
                        message for a response code $code. For example, will return &quot;Internal Server
                        Error&quot; if <var class="varname">$code</var> is 500. If <var class="varname">$http11</var> is
                        <b><tt>TRUE</tt></b> (default), will return
                        <acronym class="acronym">HTTP</acronym>/1.1 standard messages - otherwise
                        <acronym class="acronym">HTTP</acronym>/1.0 messages will be returned. If
                        <var class="varname">$code</var> is not specified, this method will return all known
                        <acronym class="acronym">HTTP</acronym> response codes as an associative (code =&gt; message)
                        array.
                    </p>
                </li>
            </ul>
        </p>

        <p class="para">
            Apart from parser methods, the class also includes a set of decoders for common
            <acronym class="acronym">HTTP</acronym> response transfer encodings:

            <ul class="itemizedlist">
                <li class="listitem">
                    <p class="para">
                         <span class="methodname">Zend_Http_Response::decodeChunkedBody($body)</span>:
                        Decode a complete &quot;Content-Transfer-Encoding: Chunked&quot; body
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                         <span class="methodname">Zend_Http_Response::decodeGzip($body)</span>: Decode
                        a &quot;Content-Encoding: gzip&quot; body
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                         <span class="methodname">Zend_Http_Response::decodeDeflate($body)</span>: Decode
                        a &quot;Content-Encoding: deflate&quot; body
                    </p>
                </li>
            </ul>
        </p>
    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.http.cookies.html">Zend_Http_Cookie and Zend_Http_CookieJar</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.http.html">Zend_Http</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.http.user-agent.html">Zend_Http_UserAgent</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="reference.html">Refer&ecirc;ncia do Zend Framework</a></li>
  <li class="header up"><a href="zend.http.html">Zend_Http</a></li>
  <li><a href="zend.http.client.html">Introduction</a></li>
  <li><a href="zend.http.client.advanced.html">Zend_Http_Client - Advanced Usage</a></li>
  <li><a href="zend.http.client.adapters.html">Zend_Http_Client - Connection Adapters</a></li>
  <li><a href="zend.http.cookies.html">Zend_Http_Cookie and Zend_Http_CookieJar</a></li>
  <li class="active"><a href="zend.http.response.html">Zend_Http_Response</a></li>
  <li><a href="zend.http.user-agent.html">Zend_Http_UserAgent</a></li>
  <li><a href="zend.http.user-agent-device.html">The UserAgent Device Interface</a></li>
  <li><a href="zend.http.user-agent-features.html">The UserAgent Features Adapter</a></li>
  <li><a href="zend.http.user-agent-features-wurfl.html">The WURFL UserAgent Features Adapter</a></li>
  <li><a href="zend.http.user-agent-features-device-atlas.html">The DeviceAtlas UserAgent Features Adapter</a></li>
  <li><a href="zend.http.user-agent-features-tera-wurfl.html">The TeraWurfl UserAgent Features Adapter</a></li>
  <li><a href="zend.http.user-agent-storage.html">The UserAgent Storage Interface</a></li>
  <li><a href="zend.http.user-agent-storage-session.html">The Session UserAgent Storage Adapter</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>